/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#ifndef ENCLAVE_H_
#define ENCLAVE_H_

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <stdbool.h>

#include <sys/stat.h>
#include <sys/mman.h>
#include <linux/mman.h>

#define MAX_VCPUS               256
#define DEFAULT_VCPUS           2
#define MAX_MEM_MB              (256 << 10)
#define MIN_MEM_MB              128
#define DEFAULT_MEM_MB          (4 << 10)
#define DEFAULT_HOST_CID        3
#define ENCLAVE_CID_MIN         4
#define ENCLAVE_CID_MAX         (4294967295 - 1)
#define DEFAULT_ENCLAVE_PORT    9000
#define ENCLAVE_BIN             "qt-enclave"

#define MIN(a, b) (((a) < (b)) ? (a) : (b))

struct enclave_img_info {
    char *kernel;
    char *initrd;
    char *cmdline;
    char *output;
    char *private_key;
    char *signing_certificate;
};

struct enclave_img_hdr {
    uint32_t magic;
    uint32_t version;
    uint64_t total_size;
    uint64_t kernel_offset;
    uint64_t kernel_size;
    uint64_t uncomp_kernel_size;
    uint64_t initrd_offset;
    uint64_t initrd_size;
    uint64_t cmdline_offset;
    uint64_t cmdline_size;
    uint64_t certificate_offset;
    uint64_t certificate_size;
    uint64_t signature_offset;
    uint64_t signature_size;
};

struct enclave_instance_info {
    int cpus;
    int64_t vsock_cid;
    int pid;
    int status;
    uint64_t memory;
    uint64_t enclave_id;
    char *image;
    int debug;
};

int enclave_img_build(struct enclave_img_info *info);
int enclave_run(struct enclave_instance_info *info);
int enclave_destroy(uint64_t enclave_id);
int enclave_describes(int64_t  enclave_id);
int enclave_console(uint64_t enclave_id);
#endif /* ENCLAVE_H_ */
